<template>
	<view class="login">
		<view class="text-center"><img class="image" src="https://xinzhu-1301398555.cos.ap-shanghai.myqcloud.com/wxapp/logo-app.jpg" alt=""></view>
		<view class="text-center mt15">欢迎登入新筑学社</view>
		<button class="default-button" @click="login()">立即登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			login(code) {
				let userRes = this.getUserInfo()
				let loginRes = this.getLogin()
				Promise.all([userRes,loginRes]).then((res) => {
					const serverUrl = 'https://api.xinzhuvip.com/api2/wxamp/onLogin';
					uni.request({
						url: serverUrl,
						method: 'POST',
						data: {
							code: res[1].code,
							encryptedData:res[0].encryptedData,
							iv:res[0].iv
						},
						success: (res) => {
							uni.setStorageSync('token',res.data.data);
							uni.navigateBack({
							    delta: 1 // 默认值是1，表示返回的页面层数
							});
						},
						fail: (error) => {
							console.log(error);
						}
					});
				})
			},
			getUserInfo(){
				return new Promise((resolve,reject) => {
					wx.getUserProfile({
						desc: '用户登录', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
						success: (res) => {
							resolve(res)
						},
						fail:(err) => {
							reject(err) 
						}
					})
				})
			},
			getLogin(){
				return new Promise((resolve,reject) => {
					wx.login({
						success (res) {
							resolve(res)
						},
						fail: (err) => {
							reject(err)
						}
					})
				})
			},
		}
	}
</script>

<style scoped>
.image{width: 120px;height: 120px;}
.login{width: 90%;margin:0px auto;margin-top: 120px;}
.default-button{border-radius: 12px;margin-top: 120px;}
</style>
